<template>
  <div>
    <!-- 添加组件 -->
    <el-dialog
      class="container"
      :title="`${!adds ? '编辑' : '新增'}`+'目录'"
      :visible="showDialog"
      @close="closeDialog"
      :close-on-click-modal="false"
    >
      <el-form v-if="data" class="addform" :model="data" :rules="rules" ref="directorysFormRef">
        <el-form-item  v-if="!$route.query.id" label="所属学科" prop="subjectName">
          <el-select
            v-model="data.subjectName"
            placeholder="请选择"
            @focus="subjectlist"
            style="width: 70%"
          >
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="item in subjectForm"
              :key="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="目录名称" prop="directoryName">
          <el-input
            v-model="data.directoryName"
            autocomplete="off"
            placeholder="请输入目录名称"
            style="width: 70%"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submitAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { simple } from '../../api/hmmm/subjects'
import { add, update } from '../../api/hmmm/directorys'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    data: {
      type: [Array, Object],
      default: () => {
        return {
          subjectName: '',
          directoryName: ''
        }
      }
    },
    adds: {
      type: Boolean,
      default: () => false
    }
  },
  watch: {
    data: {
      handler (v) {
        if (!this.adds && v) {
          v.subjectName = v.subjectForm ? '' : this.subjectForm.find(e => e.label === v.subjectName)?.value
        }
      }
    }
  },
  data () {
    return {
      // 修改的时候根据data初始化内容
      // formData: {
      //   region: this.data.region || '',
      //   directoryName: this.data.directoryName || ''
      // },
      // 学科简单列表
      subjectForm: [],
      // 校验规则
      rules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: ['blur', 'change'] },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: ['blur', 'change'] }
        ],
        subjectName: [
          { required: true, message: '请选择学科', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  mounted () {
    // 加载下拉框数据
    this.subjectlist()
  },
  methods: {
    // 在表格中添加数据
    submitAdd () {
      this.$refs.directorysFormRef.validate(async flag => {
        if (!flag) return
        // const { data } = this
        if (this.adds) {
          await add({
            // 如果是学科页面进来的 就采用学科页面的值
            subjectID: this.$route.query.id ? Number(this.$route.query.id) : this.data.subjectName,
            directoryName: this.data.directoryName
          })
          this.$message.success('添加成功')
        } else {
          await update({
            id: this.data.id,
            subjectID: this.data.subjectName,
            directoryName: this.data.directoryName
          })
          this.$message.success('编辑成功')
        }
        this.closeDialog()
        // 通知到父组件更新数据
        // this.$parent.directorysList()
        this.$route.query ? this.$parent.pagesgoto() : this.$parent.directorysList()
      })
    },
    // 点击关闭
    closeDialog () {
      this.$refs.directorysFormRef.resetFields()
      this.$parent.editItem = null
      this.$emit('update:showDialog', false)
    },
    // 获取学科简单列表
    async subjectlist () {
      const { data } = await simple()
      this.subjectForm = data
      // if (this.data) {
      //   // 设置下拉框选中值
      //   // this.data.subjectName = this.data.subjectID || ''
      //   // console.log('asd', this.data.region)
      // }
    }
  }
}
</script>

<style scoped lang="scss">
.container {
  width: 850px;
  margin: 0 auto;
}
.addform {
  margin: 0 auto;
}
.dialog-footer {
  padding-left: 210px;
}
</style>
